package com.atguigu.eduucenter.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.ordervo.UcenterMemberOrder;
import com.atguigu.eduucenter.entity.UcenterMember;
import com.atguigu.eduucenter.entity.vo.RegisterVo;
import com.atguigu.eduucenter.service.UcenterMemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author LEO
 * @since 2021-07-25
 */
@RestController
@RequestMapping("/eduucenter/ucenter-member")
@CrossOrigin
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    @PostMapping("/login")
    public R login(@RequestBody UcenterMember ucenterMember){ //ucenterMember封装手机号和密码
        //返回token，使用jwt生成
       String token= ucenterMemberService.login(ucenterMember);
        return R.ok().data("token",token);
    }

    //注册
    @PostMapping("/register")
    public R registerUser(@RequestBody RegisterVo registerVo) {
        ucenterMemberService.register(registerVo);
        return R.ok();
    }

    //根据token获取用户信息
    @GetMapping("/getMemberInfo")
    public R getMemberInfo(HttpServletRequest request) {
        //调用jwt工具类的方法。根据request对象获取头信息，返回用户id
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        //查询数据库根据用户id获取用户信息
        UcenterMember member = ucenterMemberService.getById(memberId);
        return R.ok().data("userInfo",member);
    }


    //根据用户id查询用户信息
      @PostMapping("/getUserInfoOrder/{id}")
      public UcenterMemberOrder getUserInfoOrder(@PathVariable String id){
          UcenterMember member = ucenterMemberService.getById(id);
        //  member复制给UcenterMemberOrder
          UcenterMemberOrder ucenterMemberOrder = new UcenterMemberOrder();
          BeanUtils.copyProperties(member,ucenterMemberOrder);
          return ucenterMemberOrder;
      }

      //查询某天注册人数
    @PostMapping("/countRegister/{day}")
    public R countRegister(@PathVariable String day){
    Integer count= ucenterMemberService.countRegisterDay(day);
        return R.ok().data("countRegister",count);
    }
}

